﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace LinqAndPLinq
{
    // 集合扩展类
    public static class ListExt
    {
        // 计算集合平均值
        public static int Avg(this List<int> list)
        {
            int sum = 0;
            foreach (var l in list)
            {
                sum += l;
            }
            return sum / list.Count;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello PLINQ World!");
            var list = new List<int>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(i);
            }

            var plist = list.AsParallel();
            var avg = plist.Average();
            Console.WriteLine($"PLINQ Average: {avg}");

            plist.ForAll(l => 
            {
                Console.WriteLine($"value: {l}, " +
                    $"tid: {Thread.CurrentThread.ManagedThreadId}");
            });

        }
    }
}
